package cn.gov.pbc.gd.credit.user.service.corp.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liu.jbase.PageListVo;
import com.liu.jbase.ResultVo;
import com.liu.jbase.annotation.LogAnnotation;
import com.liu.jbase.constant.ResCode;
import com.liu.jbase.service.impl.BaseServiceImpl;
import com.liu.tools.LogUtil;

import cn.gov.pbc.gd.credit.api.corp.ICorpApi;
import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.constant.LogConstant;
import cn.gov.pbc.gd.credit.common.model.UserModel;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.common.util.UserUtil;
import cn.gov.pbc.gd.credit.user.service.corp.ICorpService;
import cn.gov.pbc.gd.credit.vo.corp.CorpVo;
import cn.gov.pbc.gd.credit.vo.corp.ResCorpVo;

@Service
public class CorpServiceImpl extends BaseServiceImpl<ResCorpVo, CorpVo, ICorpApi> implements ICorpService{
	
	private static Logger logger = LoggerFactory.getLogger(CorpServiceImpl.class);
	
	@Autowired
	private ICorpApi corpApi;
	
	@Autowired
	private IRedisService redisService;
	
	@Override
	public ResultVo<PageListVo<ResCorpVo>> page(CorpVo record, PageListVo<ResCorpVo> page, HttpServletRequest request) {
		
		ResultVo<PageListVo<ResCorpVo>> resultVo = new ResultVo<>();
		UserModel userModel = UserUtil.getUserModel(record.getSid(), redisService);
		if(record.getIsQuerySelf() == null){
			record.setIsQuerySelf(1);
		}
				
		record.setParentId(userModel.getCorpId());
		PageListVo<ResCorpVo> corp= corpApi.page(record, page);
		
		resultVo.setCode(CodeConstant.SUCCESS);
		resultVo.setData(corp);
		return resultVo;
	}
	
	@LogAnnotation(moduleName=LogConstant.MODULE_CORP, option=LogConstant.ADD, logType=LogConstant.LOG_OPERATE, content="添加支行")
	@Override
	public ResultVo<ResCorpVo> add(CorpVo record, HttpServletRequest request) {
		UserModel userModel = UserUtil.getUserModel(record.getSid(), redisService);
		record.setParentId(userModel.getCorpId());
		record.setCreateBy(userModel.getUserId());
		return super.add(record, request);
	}
	
	@LogAnnotation(moduleName=LogConstant.MODULE_CORP, option=LogConstant.UPDATE, logType=LogConstant.LOG_OPERATE, content="修改支行")
	@Override
	public ResultVo<ResCorpVo> update(CorpVo record, HttpServletRequest request) {
		
		return super.update(record, request);
	}
	
	@LogAnnotation(moduleName=LogConstant.MODULE_CORP, option=LogConstant.DEL, logType=LogConstant.LOG_OPERATE, content="删除支行")
	@Override
	public ResultVo<ResCorpVo> delete(CorpVo record, HttpServletRequest request) {
		// TODO Auto-generated method stub
		return super.delete(record, request);
	}

	
	/**
	 * 此方法，通过ID,返回该行和其下级支行。
	 */
	@Override
	public ResultVo<List<ResCorpVo>> listById(String id, HttpServletRequest request) {
		ResultVo<List<ResCorpVo>> res = new ResultVo<>();
		res.setCode(ResCode.FAIL);
		try {
			
			List<ResCorpVo> list = corpApi.listById(id);

			res.setCode(ResCode.SUCCESS);
			if (list == null) {
				res.setCode(ResCode.DATA_NOT_EXSIT);
			}
			res.setData(list);
			
		} catch (Exception e) {
			res.setCode(ResCode.EXCEPTION);
			logger.error(LogUtil.msg("CorpServiceImpl:listById", corpApi.getClass().getName() + ", 查询记录异常"), e);
		}
		
		return res;
	}

	@Override
	public ResultVo<List<ResCorpVo>> treeCropList(CorpVo vo, HttpServletRequest request) {
		
		ResultVo<List<ResCorpVo>> resultVo = new ResultVo<>();
		resultVo.setCode(CodeConstant.SUCCESS);
		resultVo.setData(corpApi.treeCropList(vo));
		
		return resultVo;
	}
	
}
